var e = require("../../@babel/runtime/helpers/interopRequireDefault"), t = require("../../@babel/runtime/helpers/toConsumableArray"), a = require("../../@babel/runtime/helpers/createForOfIteratorHelper"), n = e(require("../../@babel/runtime/regenerator")), o = require("../../@babel/runtime/helpers/asyncToGenerator"), i = e(require("../../http/directBuyApi")), s = getApp(), r = (require("../../utils/util.js"), 
require("../../utils/api.js")), u = require("../../http/productApi.js"), c = require("../../http/userApi.js"), d = new i.default();

Page({
    isOnFg: !1,
    productId: -1,
    queueId: -1,
    boxId: -1,
    inLineId: -1,
    openLogined: !1,
    opening: !1,
    shakeLimit: 1,
    shakeLastTime: 0,
    shakeSpace: 2e3,
    innerAudioContext: null,
    data: {
        inited: !1,
        systemInfo: s.data.systemInfo,
        shaking: !1,
        modeScrollLeft: 0,
        showCardDialog: !1,
        productInfo: {}
    },
    onLoad: function(e) {
        var t = this;
        this.productId = e.productId, this.queueId = e.queueId, this.boxId = e.boxId, this.setData({
            requestParams: {
                productId: e.productId,
                queueId: e.queueId,
                boxId: e.boxId
            }
        }), this.getBrightBoxDetails(), this.innerAudioContext = wx.createInnerAudioContext(), 
        this.innerAudioContext.src = "/shake.mp3", wx.setInnerAudioOption({
            obeyMuteSwitch: !1
        }).catch(function(e) {}), s.data.loginInfo || wx.navigateTo({
            url: "/pages/login/login",
            success: function(e) {
                t.openLogined = !0;
            }
        });
    },
    onShow: function() {
        var e = this;
        this.isOnFg = !0, s.data.loginInfo ? this.data.inited || this.reloadDetail(!0) : this.openLogined && s.returnUI(), 
        s.globalData.callback = function(t) {
            "BRIGHT_QUEUE_SELL_OUT" == t.businessType && (console.log("盒堆被抽空"), e.boxOnEmpty());
        }, wx.startAccelerometer(), wx.onAccelerometerChange(function(t) {
            if (!(t.x < e.shakeLimit && t.y < e.shakeLimit && t.z < e.shakeLimit)) {
                var a = new Date().getTime();
                a - e.shakeLastTime < e.shakeSpace || (e.shakeLastTime = a, e.shake());
            }
        });
    },
    onHide: function() {
        this.isOnFg = !1, wx.offAccelerometerChange(), wx.stopAccelerometer();
        var e = this.selectComponent("#cd");
        e && e.stopTimer();
    },
    onUnload: function() {
        this.isOnFg = !1, wx.offAccelerometerChange(), wx.stopAccelerometer();
        var e = this.selectComponent("#cd");
        e && e.stopTimer();
    },
    boxOnEmpty: function() {
        var e = this;
        return o(n.default.mark(function t() {
            var a;
            return n.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, u.boxLineExit();

                  case 2:
                    (a = e.selectComponent("#cd")) && a.stopTimer(), e.setData({
                        cd: -1
                    });

                  case 5:
                  case "end":
                    return t.stop();
                }
            }, t);
        }))();
    },
    showCouponListFlag: function() {
        this.setData({
            couponListFlag: !0
        });
    },
    chooseCoupon: function(e) {
        var t = e.detail.id;
        this._resetCoupon(t), this.setData({
            couponListFlag: !1
        }), this.selectComponent("#buyingBev").getCouponHistoryId(t);
    },
    _resetCoupon: function(e) {
        var t, n = this.data.canUseCoupon, o = a(n);
        try {
            for (o.s(); !(t = o.n()).done; ) {
                var i = t.value;
                e ? (i.select = i.couponHistoryId == e, i.couponHistoryId == e && i) : i.select = !1;
            }
        } catch (e) {
            o.e(e);
        } finally {
            o.f();
        }
        this.setData({
            canUseCoupon: n
        });
    },
    changeCouponListFlag: function(e) {
        e.detail.canUseCoupon ? this.setData({
            couponListFlag: !this.data.couponListFlag,
            canUseCoupon: e.detail.canUseCoupon,
            noCanUseList: e.detail.noCanUseList
        }) : this.setData({
            couponListFlag: !this.data.couponListFlag
        }), wx.hideLoading();
    },
    getBrightBoxDetails: function() {
        var e = this;
        return o(n.default.mark(function t() {
            var a;
            return n.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, d.getbrightBoxDetails(e.data.requestParams);

                  case 2:
                    a = t.sent, e.setData({
                        productInfo: a.data
                    });

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, t);
        }))();
    },
    closeCardDialogOnTap: function() {
        this.setData({
            showCardDialog: !1
        });
    },
    showCardDialogOnTap: function(e) {
        var t = e.currentTarget.dataset.type;
        this.setData({
            showCardDialog: !0,
            curCardType: t
        });
    },
    changeOnTap: function() {
        var e = this;
        return o(n.default.mark(function t() {
            var a;
            return n.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return e.setData({
                        shakeTips: null
                    }), t.prev = 1, s.showLoading(), t.next = 5, u.boxChange(e.productId, e.queueId, e.boxId);

                  case 5:
                    !(a = t.sent).data && a.msg ? s.showAlert("提示", a.msg) : (e.setData({
                        inited: !1
                    }), e.boxId = a.data.boxId, e.reloadDetail()), t.next = 12;
                    break;

                  case 9:
                    t.prev = 9, t.t0 = t.catch(1), console.log(t.t0);

                  case 12:
                    return t.prev = 12, wx.hideLoading(), t.finish(12);

                  case 15:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 1, 9, 12, 15 ] ]);
        }))();
    },
    closeOnTap: function() {
        s.returnUI();
    },
    continueOnTap: function() {
        this.data.lastCd > 0 ? (s.showLoading(), r.modelOpenContinue(s.getUsername(), s.getToken(), this.inLineId).catch(function(e) {}).then(function(e) {
            wx.hideLoading(), s.returnUI();
        })) : s.returnUI();
    },
    shareOnTap: function() {
        var e = this;
        return o(n.default.mark(function t() {
            var a;
            return n.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, t.next = 3, c.getOpenBrightBoxShareCount();

                  case 3:
                    a = t.sent, e.setData({
                        showShareDialog: !0,
                        shareInfo: {
                            rule: "分享链接后被不同的玩家点击每次获得5心愿值",
                            todayCurrent: a.data.shareOpenBrightBoxProductCount,
                            todayTotal: a.data.taskNum
                        }
                    }), t.next = 9;
                    break;

                  case 7:
                    t.prev = 7, t.t0 = t.catch(0);

                  case 9:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 0, 7 ] ]);
        }))();
    },
    closeShareDialog: function() {
        this.setData({
            showShareDialog: !1
        });
    },
    closeBuyDetailOnTap: function(e) {
        e.detail.paySuccess, this.setData({
            showBuyDetailDialog: !1
        });
    },
    showTimesDetailListOnTap: function() {
        this.setData({
            showTimesDetailList: !0
        });
    },
    closeTimesDetailList: function() {
        this.setData({
            showTimesDetailList: !1
        });
    },
    closeBuyListOnTap: function() {
        this.setData({
            showBuyListDialog: !1
        });
    },
    buyTimesOnTap: function(e) {
        this.setData({
            showBuyListDialog: !1,
            showBuyDetailDialog: !0,
            priceInfo: e.detail
        });
    },
    showSendAgreement: function() {
        this.setData({
            showSendAgreementDialog: !0
        });
    },
    closeSendAgreementOnTap: function() {
        this.setData({
            showSendAgreementDialog: !1
        });
    },
    agreeSendAgreementOnTap: function() {
        this.closeSendAgreementOnTap(), wx.navigateTo({
            url: "/pages/edit_address/edit_address"
        });
    },
    cdOnEnd: function() {
        var e = this;
        this.isOnFg && (wx.offAccelerometerChange(), wx.stopAccelerometer(), wx.showModal({
            title: "重新排队提示",
            content: "等待时间过长，已被移出队列，是否继续排队？",
            success: function(t) {
                t.confirm ? (e.getOpenerEventChannel().emit("cdOnEnd", {}), s.returnUI()) : wx.reLaunch({
                    url: "/pages/index/index"
                });
            }
        }));
    },
    modelItemOnTap: function(e) {},
    showCouponListOnTap: function(e) {
        this.setData({
            couponListFlag: !0,
            canUseCoupon: e.detail.canUseCoupon,
            noCanUseList: e.detail.noCanUseList
        });
    },
    closeCouponListOnTap: function() {
        this.setData({
            couponListFlag: !1
        });
    },
    couponOnSelected: function(e) {
        console.log("couponOnSelected", e.detail);
        var t = e.detail.id, a = null;
        this.data.canUseCoupon.map(function(e) {
            e.couponHistoryId == t && (a = e);
        }), this.setData({
            couponListFlag: !1,
            couponSelected: a
        });
    },
    cancelCoupon: function() {
        this._resetCoupon(), this.selectComponent("#buyingBev").getCouponHistoryId();
    },
    couponOnNotSelect: function() {
        this.setData({
            couponListFlag: !1,
            couponSelected: null
        });
    },
    onOpenSuccess: function(e) {
        this.setData({
            showBuyDetailDialog: !1,
            resultInfo: e.detail
        });
    },
    openOnTap: function(e) {
        this.setData({
            showBuyDetailDialog: !0,
            smallImage: s.data.boxStaticInfo.productPic,
            couponSelected: null,
            priceInfo: {
                Price: s.data.boxStaticInfo.mallPrice,
                isacting: 0 == s.data.boxStaticInfo.meetStatus ? 1 : 0,
                newPrice: s.data.boxStaticInfo.newRecruitsActivityPrice
            }
        });
    },
    reloadDetail: function(e) {
        var t = this;
        return o(n.default.mark(function a() {
            var o, i, r;
            return n.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    return e && wx.showLoading({
                        title: "加载中…"
                    }), a.prev = 1, a.next = 4, u.myLineInfo();

                  case 4:
                    return o = a.sent, i = -1, o.data.overDate && (i = parseInt((o.data.overDate - o.data.thisDate) / 1e3)), 
                    a.next = 9, u.boxSkuList(t.productId, t.queueId, t.boxId);

                  case 9:
                    return o = a.sent, a.next = 12, s.downloadUserInfo();

                  case 12:
                    t.setData({
                        inited: !0,
                        productId: t.productId,
                        boxId: t.boxId,
                        cd: i,
                        title: s.data.boxStaticInfo.productTitle,
                        bigImage: s.data.boxStaticInfo.brightBoxAttr.boxPileBigImg,
                        modelList: o.data,
                        wishValue: s.data.loginInfo.userInfo.availableWish,
                        boxType: s.data.boxStaticInfo.brightBoxAttr.boxType,
                        showCardCount: s.data.loginInfo.userInfo.displayCardNumber,
                        showCardInfo: {
                            useValue: 100,
                            desc: '消耗100心愿值来显示里面是什么\n获得更多心愿值，去做"任务"吧'
                        },
                        noticeCardCount: s.data.loginInfo.userInfo.tipCardNumber,
                        noticeCardInfo: {
                            useValue: 10,
                            desc: "消耗10心愿值使用一张提示卡\n可额外获得一次提示信息"
                        }
                    }), (r = t.selectComponent("#cd")) && r.init(), a.next = 20;
                    break;

                  case 17:
                    a.prev = 17, a.t0 = a.catch(1), console.log(a.t0);

                  case 20:
                    return a.prev = 20, wx.hideLoading(), a.finish(20);

                  case 23:
                  case "end":
                    return a.stop();
                }
            }, a, null, [ [ 1, 17, 20, 23 ] ]);
        }))();
    },
    shake: function() {
        var e = this;
        return o(n.default.mark(function t() {
            var a, o, i, s;
            return n.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    if (!e.data.shaking) {
                        t.next = 2;
                        break;
                    }
                    return t.abrupt("return");

                  case 2:
                    return !1 === wx.getStorageSync("VoiceToggle") || e.innerAudioContext && e.innerAudioContext.play(), 
                    e.setData({
                        shaking: !0,
                        shakeTips: null
                    }), t.prev = 5, a = 1200, o = new Date().getTime(), t.next = 10, u.boxShake(e.productId, e.queueId, e.boxId);

                  case 10:
                    if (i = t.sent, !((s = new Date().getTime() - o) < a)) {
                        t.next = 15;
                        break;
                    }
                    return t.next = 15, r.delay(o, a - s);

                  case 15:
                    i.data ? e.tipResult(i.data) : e.tipText(i.msg), t.next = 21;
                    break;

                  case 18:
                    t.prev = 18, t.t0 = t.catch(5), console.log(t.t0);

                  case 21:
                    return t.prev = 21, e.setData({
                        shaking: !1
                    }), t.finish(21);

                  case 24:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 5, 18, 21, 24 ] ]);
        }))();
    },
    useCardOnTap: function(e) {
        var t = this;
        return o(n.default.mark(function a() {
            var o, i, r, d;
            return n.default.wrap(function(a) {
                for (;;) switch (a.prev = a.next) {
                  case 0:
                    if (o = e.currentTarget.dataset.type, wx.showLoading({
                        title: "加载中…"
                    }), a.prev = 2, 1 != o) {
                        a.next = 16;
                        break;
                    }
                    return a.next = 6, u.boxUseTipCard(t.productId, t.queueId, t.boxId);

                  case 6:
                    if ((i = a.sent).data) {
                        a.next = 13;
                        break;
                    }
                    return t.tipText(i.msg), t.setData({
                        showCardDialog: !1
                    }), a.abrupt("return");

                  case 13:
                    t.tipResult(i.data);

                  case 14:
                    a.next = 27;
                    break;

                  case 16:
                    if (2 != o) {
                        a.next = 27;
                        break;
                    }
                    return a.next = 19, u.boxUseShowCard(t.productId, t.queueId, t.boxId);

                  case 19:
                    if ((r = a.sent).data) {
                        a.next = 26;
                        break;
                    }
                    return t.tipText(r.msg), t.setData({
                        showCardDialog: !1
                    }), a.abrupt("return");

                  case 26:
                    t.showResult(r.data);

                  case 27:
                    return a.next = 29, c.userInfo();

                  case 29:
                    d = a.sent, s.saveUserInfo(d.data), t.setData({
                        showCardDialog: !1,
                        wishValue: s.data.loginInfo.userInfo.availableWish,
                        showCardCount: s.data.loginInfo.userInfo.displayCardNumber,
                        noticeCardCount: s.data.loginInfo.userInfo.tipCardNumber
                    }), a.next = 37;
                    break;

                  case 34:
                    a.prev = 34, a.t0 = a.catch(2), console.log(a.t0);

                  case 37:
                    return a.prev = 37, wx.hideLoading(), a.finish(37);

                  case 40:
                  case "end":
                    return a.stop();
                }
            }, a, null, [ [ 2, 34, 37, 40 ] ]);
        }))();
    },
    tipResult: function(e) {
        var t = this.findIndexByid(e.notMeSkuId);
        t >= 0 && (this.data.modelList[t].filtrTag = 1, this.moveToFirst(t)), this.tipText("根据小派派的多年经验，\n这个不是" + e.notMeSkuTitle);
    },
    showResult: function(e) {
        var t = this.findIndexByid(e.meSkuId);
        t >= 0 && (this.data.modelList.map(function(e) {
            e.filtrTag = 1;
        }), this.data.modelList[t].filtrTag = 2, this.moveToFirst(t)), this.tipText("根据小派派的多年经验，\n这个应该是" + e.meSkuTitle);
    },
    findIndexByid: function(e) {
        for (var t = 0; t < this.data.modelList.length; t++) if (this.data.modelList[t].skuId == e) return t;
        return -1;
    },
    moveToFirst: function(e) {
        var a = t(this.data.modelList), n = a.splice(e, 1);
        a.splice(0, 0, n[0]), this.setData({
            modelList: a,
            modeScrollLeft: 0
        });
    },
    tipText: function(e) {
        var t = this;
        this.setData({
            shakeTips: e
        }), null != this.shakeTipsHideTimer && clearTimeout(this.shakeTipsHideTimer), this.shakeTipsHideTimer = setTimeout(function() {
            t.setData({
                shakeTips: null
            });
        }, 5e3);
    },
    onShareAppMessage: function(e) {
        return this.data.resultInfo ? {
            title: "我在19八3抽到[" + this.data.resultInfo.skuTitle + "]，快来抽盲盒吧",
            imageUrl: this.data.resultInfo.shareImg,
            path: "/pages/series/series?productId=" + this.productId + "&queueId=" + this.queueId + "&fromType=1&shareFrom=" + s.getUsername() + "&fromUserID=" + s.getUsername()
        } : {
            title: s.data.boxStaticInfo.brightBoxAttr.shareTile,
            imageUrl: s.data.boxStaticInfo.brightBoxAttr.shareImg,
            path: "/pages/series/series?productId=" + this.productId + "&queueId=" + this.queueId + "&fromType=1&shareFrom=" + s.getUsername() + "&fromUserID=" + s.getUsername()
        };
    }
});